<?
include_once "./naiveBayesClassifierEv.php";
include_once "./NearestNeighbourClassifierEv.php";

$ev = new Evaluation();
$ev -> classifyAll();


class Evaluation {

	public function __construct() {}

	public function classifyAll() {
		
		$naiveBayes = new NaiveBayesClassifier();

		$negTestSet = file("./ev/NegativeTestSetEv.txt");
		$posTestSet = file("./ev/PositiveTestSetEv.txt");
		
//		echo '<pre>'.print_r($negTestSet, true).'</pre>';

		$TP = 0;
		$TN = 0;
		$FN = 0;
		$FP = 0;
		
		foreach ($negTestSet as $curr) {
			$sentence = $this -> extractWordsFromSentence($curr);
			$score = $naiveBayes -> classify($sentence);
			
			if ($score=="neg")
				$TN++;
			else
				$FP++;			
		}

		foreach ($posTestSet as $curr) {
			$sentence = $this -> extractWordsFromSentence($curr);
			$score = $naiveBayes -> classify($sentence);
			
			if ($score=="pos")
				$TP++;
			else
				$FN++;			
		}
		
		echo "TP : $TP <br>";
		echo "TN : $TN <br>";
		echo "FN : $FN <br>";
		echo "FP : $FP <br>";

		$accuracy = ($TP + $TN) / ($TP + $TN + $FP + $FN); 
		echo "Accuracy: $accuracy <br>";
		
		$precision = $TP / ($TP + $FP);
		echo "Precision: $precision <br>";


		$nearestNeighbour = new NearestNeighbourClassifier(3);

		$objTestSet = json_decode(file_get_contents("./ev/testSetObjEV.txt"), true);
		$subjTestSet = json_decode(file_get_contents("./ev/testSetSubjEV.txt"), true);

		$TP = 0;
		$TN = 0;
		$FN = 0;
		$FP = 0;

		$i = 0;		
		foreach ($objTestSet as $curr) {
			if ($i++ > 50)
				break;
			$score = $nearestNeighbour -> classify($curr);
		
			if ($score=="obj")
				$TN++;
			else
				$FP++;			
		}

		$i = 0;
		foreach ($subjTestSet as $curr) {
			if ($i++ > 50)
				break;
			$score = $nearestNeighbour -> classify($curr);
			
			if ($score=="subj")
				$TP++;
			else
				$FN++;			
		}
		
		echo "TP : $TP <br>";
		echo "TN : $TN <br>";
		echo "FN : $FN <br>";
		echo "FP : $FP <br>";

		$accuracy = ($TP + $TN) / ($TP + $TN + $FP + $FN); 
		echo "Accuracy: $accuracy <br>";
		
		$precision = $TP / ($TP + $FP);
		echo "Precision: $precision <br>";


	}

	private function extractWordsFromSentence($sentence)
	{    
		$sentence = strtolower($sentence);
	
		preg_match_all('/[a-zA-Z]+(\'[a-zA-Z]+)?/i', $sentence, $matches);
		
		for ($i = 0; $i < count($matches[0]); $i++)
			$matches[0][$i] = str_replace('_', '', $matches[0][$i]);
	
		return $matches[0];

		// We apply the changes
		$withoutPonctuation = trim(str_replace($toreplace, $replacedBy, $sentence)); 
		// We put every words in an array
		$separator = "[ ]+"; // 1 or more space
		$words = split($separator, $withoutPonctuation); 
		return $words;
	}



}

/*
$dataSetSubj = json_decode(file_get_contents("./src/dataSetSubj.txt"), true);
$dataSetObj = json_decode(file_get_contents("./src/dataSetObj.txt"), true);

echo '<pre>'.print_r($dataSetSubj, true).'</pre>';

$dataSetSubjEv = array_slice($dataSetSubj, 0, round(count($dataSetSubj) * 0.7));
$dataSetObjEv = array_slice($dataSetObj, 0, round(count($dataSetObj) * 0.7));

$fh = fopen('./ev/dataSetSubjEV.txt', 'w');
fwrite($fh, json_encode($dataSetSubjEv));
fclose($fh);

$fh = fopen('./ev/dataSetObjEV.txt', 'w');
fwrite($fh, json_encode($dataSetObjEv));
fclose($fh);

$dataSetSubjEv = array_slice($dataSetSubj, round(count($dataSetSubj) * 0.7) + 1, count($dataSetSubj) - 1);
$dataSetObjEv = array_slice($dataSetObj, round(count($dataSetObj) * 0.7) + 1, count($dataSetObj) - 1);

$fh = fopen('./ev/testSetSubjEV.txt', 'w');
fwrite($fh, json_encode($dataSetSubjEv));
fclose($fh);

$fh = fopen('./ev/testSetObjEV.txt', 'w');
fwrite($fh, json_encode($dataSetObjEv));
fclose($fh);
*/


?>